//
//  LogisticsViewController.m
//  FreightAssistant
//
//  Created by leo on 15/12/6.
//  Copyright © 2015年 leo. All rights reserved.
//

#import "LogisticsListViewController.h"

#import "LogisticsEditViewController.h"

static NSString *LogisticsItemCellIdentifier = @"LogisticsItemCellIdentifier";

@interface LogisticsListViewController () {
    KKSearchBar *_kk_searchBar;
    
    KKLogisticsItemList *_logisticsItemList;
}

@end

@implementation LogisticsListViewController

- (void)loadView {
    [super loadView];
    
    _kk_searchBar = [[KKSearchBar alloc] initWithFrame: CGRectMake(0, 0, CGRectGetWidth(self.tableView.bounds), 40)];
    _kk_searchBar.placeholder = @"搜索";
    self.tableView.tableHeaderView = _kk_searchBar;
    self.tableView.tableFooterView = [UIView new];
}

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view from its nib.
    self.navigationItem.title = @"快递商";
    
    if (self.viewcontrollerType == KKViewControllerTypeEdit) {
        
        UIBarButtonItem *rightItem = [[UIBarButtonItem alloc] initWithBarButtonSystemItem: UIBarButtonSystemItemAdd target: self action: @selector(actionOfAdd:)];
        rightItem.tintColor = [UIColor whiteColor];
        
        self.navigationItem.rightBarButtonItem = rightItem;
    }
    else {
//        UIBarButtonItem *rightItem = [[UIBarButtonItem alloc] initWithBarButtonSystemItem: UIBarButtonSystemItemSave target: self action: @selector(actionOfSave:)];
//        rightItem.tintColor = [UIColor whiteColor];
//        
//        self.navigationItem.rightBarButtonItem = rightItem;
    }
    
    _logisticsItemList = [KKLogisticsItemList new];
}

- (void)viewWillAppear:(BOOL)animated {
    [super viewWillAppear: animated];
    
    [self downloadItemsFromDBWithSearchText: nil];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

#pragma mark - Action methods

- (void)actionOfAdd:(UIBarButtonItem*)item {
    
    LogisticsEditViewController *editViewController = [LogisticsEditViewController viewControllerFromStoryboard];
    [self.navigationController pushViewController: editViewController animated: true];
}

- (void)actionOfSave:(UIBarButtonItem*)item {
    
//    if (self.editResultBlock) {
//        self.editResultBlock(_selectedLogisticsItem);
//    }
//    [self.navigationController popViewControllerAnimated: true];
}

#pragma mark - Private methods

- (void)downloadItemsFromDBWithSearchText:(NSString*)searchText {
    
    __weak typeof(_logisticsItemList) weakItemList = _logisticsItemList;
    __weak UITableView *weakTableView = self.tableView;
    
    [KKDataBaseAPIInterface searchLogisticsItemsWithSearchText: searchText withEnabled: true successful:^(id object) {
        
        [weakItemList appendItemsWithArray: object];
        [weakTableView reloadData];
    } faile: nil];
}

- (void)deleteItemFromDBWithIndexPath:(NSIndexPath*)indexPath {
    
    KKLogisticsItem *item = [_logisticsItemList objectAtIndex: indexPath.row];
    
//    NSString *identifier = item.kk_identifier;
//    if (identifier) {
//        
//        NSDictionary *condition = @{@"identifier":identifier};
//        
//        __weak typeof(_logisticsItemList) weakItemList = _logisticsItemList;
//        __weak UITableView *weakTableView = self.tableView;
//        [[KKDataBaseManager defaultManager] removeRecordWithTableName: KKDataBase_TableName_LogisticsInfo withCondition: condition finishHandle:^(id object, BOOL successful) {
//            NSLog(@"object = %@ | successful = %i", object, successful);
//            if (successful) {
//                [weakItemList removeObjectOfIndex: indexPath.row];
//                [weakTableView deleteRowsAtIndexPaths: @[indexPath] withRowAnimation: UITableViewRowAnimationAutomatic];
//            }
//        }];
//    }
}

#pragma mark - UITableView methods

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    
    return _logisticsItemList.count;
}

- (UITableViewCell*)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier: LogisticsItemCellIdentifier];
    
    if (cell == nil) {
        cell = [[UITableViewCell alloc] initWithStyle: UITableViewCellStyleSubtitle reuseIdentifier: LogisticsItemCellIdentifier];
    }
    
    KKLogisticsItem *item = [_logisticsItemList objectAtIndex: indexPath.row];
    
    cell.textLabel.text = item.kk_name;
    cell.detailTextLabel.text = item.kk_phone;
    
    return cell;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    
    
    if (self.viewcontrollerType == KKViewControllerTypeChoose) {
        
        KKLogisticsItem *item = [_logisticsItemList objectAtIndex: indexPath.row];
        
        if (self.editResultBlock) {
            self.editResultBlock(item);
        }
        [self.navigationController popViewControllerAnimated: true];
//        _selectedLogisticsItem = item;
    }
    else {
        
        LogisticsEditViewController *editViewController = [LogisticsEditViewController viewControllerFromStoryboard];
        [self.navigationController pushViewController: editViewController animated: true];
    }
}

- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath {
    
    if (editingStyle == UITableViewCellEditingStyleDelete) {
        
        [self deleteItemFromDBWithIndexPath: indexPath];
    }
}

@end
